// ITI 1120 Fall 2006, Lab 8, Exercise 2 // Name: Diana Inkpen, 123456 /** * PROGRAM: a recursive algorithm to create an array containing the values 0 * to N-1. */ class CreateArray { public static void main (String[] args) { // DECLARE VARIABLES/DATA DICTIONARY int n; // The size of the array int[] a; // The array to be created and filled. int ix; // index used to display the contents of the array // PRINT OUT IDENTIFICATION INFORMATION System.out.println(); System.out.println("ITI 1120 Fall 2006, Lab 8, Exercise 2"); System.out.println("Name: Diana Inkpen, Student# 123456"); System.out.println(); // READ IN GIVENS System.out.print("Please enter the size of the array: "); n = ITI1120.readInt(); // BODY OF ALGORITHM a = createArray(n); // PRINT OUT RESULTS AND MODIFIEDS System.out.print("The array is: "); for( ix = 0; ix < n; ix = ix+1) { System.out.print(a[ix] + " "); } System.out.println( ); } // If the 'main' method calls other algorithms, put the method(s) below. /** * METHOD: createArray * * A recursive method to fill an array with values 0 up to n=1. * * GIVENS: n -- number of positions in array * RESULTS: a -- the new array */ public static int[] createArray(int n) { // DECLARE VARIABLES/DATA DICTIONARY int[] a; // RESULT: The new array // BODY OF ALGORITHM a = new int[n]; initArray( a, n ); // RETURN RESULT return a; } /** * METHOD: initArray * * A recursive method to fill an array with values 1 up to n. * * GIVENS: a -- reference to a previously created array with at least n positions * n -- number of elements in the array * RESULTS: (none) */ public static void initArray( int[] a, int n ) { // BODY OF ALGORITHM if ( n == 0 ) { ; // do nothing } else { initArray( a, n-1 ); a[n-1] = n; } // RETURN RESULT return; } }